package com.situ.mall.admin.controller;

import com.situ.mall.common.entity.Product;
import com.situ.mall.common.service.IAttachmentService;
import com.situ.mall.common.service.IProductService;
import com.situ.mall.common.vo.EditProductVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IAttachmentService attachmentService;

    @GetMapping("/page")
    public String page(){
        return "product";
    }

    @GetMapping("/select")
    @ResponseBody
    public TableVO select(Integer page, Integer limit, String name, String status){
        return productService.select(page,limit,name,status);
    }

    @PostMapping("/delete")
    @ResponseBody
    public void delete(String ids){
        productService.delete(ids);
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(Product product){
        productService.saveOrUpdate(product);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public EditProductVO info(@PathVariable Integer id){
        return productService.edit(id);

    }
    @PostMapping("/status")
    @ResponseBody
    public void status(Integer id,String status){
        Product p = new Product();
        p.setId(id);
        p.setStatus(status);
        productService.saveOrUpdate(p);
    }

    @PostMapping("/bind")
    @ResponseBody
    public void bind(Integer id,String attIds){
        attachmentService.bind(attIds,id,"product");
    }
}
